package hottargui.framework;

import hottargui.standard.NullPlayer;
import hottargui.standard.StandardPlayer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GameData {
	List<Tile> boardConfiguration;
	Map<PlayerColor, Player> players;
	int roundsLeft;

	Player currentPlayer;

	int lastDieRoll;

	public GameData() {
		boardConfiguration = new ArrayList<Tile>();
		players = new HashMap<PlayerColor, Player>();
	}

	public List<Tile> getBoardConfiguration() {
		return this.boardConfiguration;
	}

	public Player getPlayerInTurn() {
		return currentPlayer;
	}

	public PlayerColor setPlayerInTurn(PlayerColor newPlayerColor) {
		if(newPlayerColor != PlayerColor.None)
		{
			currentPlayer = players.get(newPlayerColor);
		}
		else{
			currentPlayer = new NullPlayer();
		}
		return newPlayerColor;
	}

	public Map<PlayerColor, Player> getPlayers() {
		return players;
	}

	public int getRoundsLeft() {
		return roundsLeft;
	}

	public void setRoundsLeft(int newRoundsLeft) {
		roundsLeft = newRoundsLeft;
	}

	public void decreaseRoundsLeft() {
		roundsLeft--;
	}

	public void setLastDieRoll(int dieRoll) {
		lastDieRoll = dieRoll;
	}

	public int getLastDieRoll() {
		return lastDieRoll;
	}

	public Tile getTile(int row, int col) {
		for (Tile tile : boardConfiguration) {
			if (tile.getPosition().getRow() == row && tile.getPosition().getColumn() == col)
				return tile;
		}
		return null;
	}

	public int getRevenue(Player player) {
		int revenue = 0;
		for (Tile tile : boardConfiguration) {
			if (tile.getOwnerColor() == player.getColor()) {
				revenue += tile.getType().getEconomicValue();
			}
		}

		/* no home settlement, so no revenue collected */
		Position settlementPos = ((StandardPlayer) player).getSettlement();
		Tile settlement = getTile(settlementPos.getRow(), settlementPos.getColumn());
		if (settlement.getOwnerColor() != player.getColor()) {
			revenue = 0;
		}

		return revenue;
	}
}
